<?php
/**
 * @监控类
 * 1、监控列表
 * 2、监控添加或者移除
 * 3、监控的舆情list
 * @作者
 * 万全强（php）
 */
namespace app\controller;

use app\controller\HomeBase;
use think\facade\Db;


use app\validate\MonitorValidate;
use app\model\MonitorModel;
use app\model\ProjectsModel;


class Monitor extends HomeBase
{
	
	private $MonitorValidate;
	private $MonitorModel;
	private $RoleId=0;
	
	public function __construct(){
		
		parent::__construct();
		
		$this->MonitorValidate=new MonitorValidate();
		$this->MonitorModel=new MonitorModel();
		
	}
	/**
	 * 监控列表
	 *
	 * @return void
	 */
	public function GetMonitorList()
	{
		$param=request()->param();
		$where="1=1 and status =1 ";
		if(isset($param['keywords'])&&!empty($param['keywords'])){
			$where.=" and name like '%".trim($param['keywords'])."%'";
		}
		if(isset($param['type'])&&!empty($param['type'])){
			$where.=" and type = ".intval($param['type']);
		}
		$where.=" and user_id=".$this->user_id;
		
		$res=$this->MonitorModel->GetList($where);
		return json(['status'=>1,'msg'=>'获取成功','data'=>$res]);
	}
	
	/**
	 * 监控添加/编辑
	 *
	 * @return void
	 */
	public function MonitorAddOrCancel()
	{
		$param=request()->param();
		$identification=isset($param['identification'])?trim($param['identification']):'';
		$type=isset($param['type'])?intval($param['type']):0;
		//表单验证
		try {
			$re=$this->validate($param,'app\validate\MonitorValidate');
		} catch (\Exception $e) {
			return json(['status'=>0,'msg'=>$e->error,'data'=>[]]);
		}
		$param['user_id']=$this->user_id;
		//判断是否存在
		$info=$this->MonitorModel->GetDetail($param);
		//管理表
		if($info){
			$this->MonitorModel->UpdateStatus($info['id']);
			
			//来自项目
			if($info['project_id']!=0){
				$ProjectsModel=new ProjectsModel();
				$ProjectsModel->EditData(['is_monitor'=>0,$info['project_id'],['is_monitor']]);
			}
		}else{
			$id=$this->MonitorModel->AddData($param);
			
			//来自项目
			if(isset($param['project_id'])&&!empty($param['project_id'])){
				$ProjectsModel=new ProjectsModel();
				$ProjectsModel->EditData(['is_monitor'=>1],$param['project_id'],['is_monitor']);
			}
		}
		
		return json(['status'=>1,'msg'=>'操作成功','data'=>['IsMonitor'=>empty($info)]]);
	}
	/**
	 * 监控舆情
	 *
	 * @return void
	 */
	public function GetMonitorNewsList()
	{
		$param=request()->param();
		$id=isset($param['id'])?trim($param['id']):'';
		
		$result=$this->MonitorModel->Del(explode(',',$id));
		if($result){
			return json(['status'=>1,'msg'=>'删除成功','data'=>[]]);
		}else{
			return json(['status'=>0,'msg'=>'删除失败','data'=>[]]);
		}
	}
}
